<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/25
 * Time: 14:05
 */

namespace app\bis\controller;

/*
 * 门店管理
 */
class Location extends Base
{
    private $obj;
    public function _initialize() {
        $this->obj = model("BisLocation");
    }

    /**
     * 商户列表  status=1
     */
    public function index() {
        $bislocation = $this->obj->getBisLocationByStatus(1);
        return $this->fetch('', [
            'bislocation' => $bislocation,
        ]);
    }

    /**
     * 待审核商户列表  status=0
     */
    public function apply() {
        $bislocation = $this->obj->getBisLocationByStatus(0);
        return $this->fetch('', [
            'bislocation' => $bislocation,
        ]);
    }

    /*
     * 新增门店
     */
    public function add()
    {
       if (request()->isPost()){
           $data = input('post.','','Htmlentities');

           $validate=validate('BisLocation');
           if (!$validate->scene('add')->check($data)){
               $this->error($validate->getError());
           }

           $bisId=$this->getLoginUser()->bis_id;

           $data['cat'] = '';
           if(!empty($data['se_category_id'])) {
               $data['cat'] = implode('|', $data['se_category_id']);
           }

           // 获取经纬度
           $lnglat = \Map::getLngLat($data['address']);

            //分店数据入库  is_main = 0
           $locationData = [
               'bis_id' => $bisId,
               'name' => $data['name'],
               'logo' => $data['logo'],
               'tel' => $data['tel'],
               'contact' => $data['contact'],
               'category_id' => $data['category_id'],
               'category_path' => $data['category_id'] . ',' . $data['cat'],
               'city_id' => $data['city_id'],
               'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'].','.$data['se_city_id'],
               'api_address' => $data['address'],
               'open_time' => $data['open_time'],
               'content' => empty($data['content']) ? '' : $data['content'],
               'is_main' => 0,
               'xpoint' => empty($lnglat['result']['location']['lng']) ? '' : $lnglat['result']['location']['lng'],
               'ypoint' => empty($lnglat['result']['location']['lat']) ? '' : $lnglat['result']['location']['lat'],
           ];
           $locationId = model('BisLocation')->add($locationData);
           if($locationId) {
               return $this->success('门店申请成功');
           }else {
               return $this->error('门店申请失败');
           }
       }

        $citys = model('City')->getNormalCitysByParentId();
        $categorys = model('Category')->getNormalCategoryByParentId();
        return $this->fetch('', [
            'citys' => $citys,
            'categorys' => $categorys,
        ]);
    }

    /**
     * 商家申请信息详情页
     * @return mixed|void
     */
    public function detail() {
        $id=input('param.id');
        echo 'id : '.$id   ;
        return $this->fetch();
    }
}